package tianqi.controller.shop;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import tianqi.entity.ShopCar;
import tianqi.service.shop.ShopCarService;
import tianqi.service.shop.impl.ShopCarServiceImpl;
import tianqi.utils.StringUtils;

@Controller
public class ShopCarController {

	@Autowired
	private ShopCarServiceImpl shopCarServiceImpl;

	@RequestMapping("/goShopCar")
	public String goShopCar(HttpServletRequest req, HttpServletResponse res, Model m) {

		return shopCarServiceImpl.queryCar(req, res, m);
	}
	
	@RequestMapping("/queryCarNum")
	@ResponseBody
	public String queryCarNum(HttpServletRequest req, HttpServletResponse res) {
		System.out.println("====================================================================");
		
		 JSONObject json=JSONObject.fromObject(shopCarServiceImpl.queryCarNum(req, res)); 
		 
		return json.toString();
	}
	
	/**
	 * 库存校验
	 * @param id
	 * @param num
	 * @param spec
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/checkStock")
	@ResponseBody
	public String checkStock(String id,String num,String spec,HttpServletRequest req, HttpServletResponse res,Model m) {
		//库存校验
		Map map=new HashMap<String,String>();
		map.put("id", "'"+id+"'");
		map.put("spec", "'"+spec+"'");
		Integer querySpec = shopCarServiceImpl.querySpec(map);
		if (querySpec>=Integer.valueOf(num)) {
			return "true";
		}else {
			return "false";
		}
	}
	
	@RequestMapping("/addNum")
	public String addNum(String id,String num,String spec,HttpServletRequest req, HttpServletResponse res,Model m) {
			shopCarServiceImpl.addNum(id, num,spec, req, res);
			
			return "redirect:goShopCar.tq";
	}
	@RequestMapping("/deleteCar")
	public String deleteCar(String id,String spec,HttpServletRequest req, HttpServletResponse res,Model m) {
		shopCarServiceImpl.deleteCar(id,spec, req, res);
		
		return "redirect:goShopCar.tq";
	}
	@RequestMapping("/clearCar")
	public String clearCar(HttpServletRequest req, HttpServletResponse res,Model m) {
		shopCarServiceImpl.clearCar(req, res);
		
		return "redirect:goShopCar.tq";
	}

	

	@RequestMapping("/addShopCar")
	@ResponseBody
	public String addShopCar(String id,String num,String spec,HttpServletRequest req, HttpServletResponse res,Model m) {
		try {
			//并加上之前cookie 中的数量 
			num=name(req, num, id,spec);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//库存校验
		Map map=new HashMap<String,String>();
		map.put("id", "'"+id+"'");
		map.put("spec", "'"+spec+"'");
		//  只是把最小销售单元的   库存信息查询出来
		Integer querySpec = shopCarServiceImpl.querySpec(map);
		if (querySpec>=Integer.valueOf(num)) {
			shopCarServiceImpl.addCar(id, num,spec, req, res);
			return "true";
		}else {
			return "false";
		}
	}

	public static String name(HttpServletRequest req, String num, String id, String spec) throws UnsupportedEncodingException {
		List<ShopCar> list = new ArrayList<ShopCar>();
		Cookie[] cookies = req.getCookies();
		if (cookies != null || cookies.length > 0) {
			// 创建json的配置对象
			JsonConfig jConfig = new JsonConfig();
			String[] excludes = { "pro", "detailImg" };
			// 排除 pro 这一属性
			jConfig.setExcludes(excludes);
			jConfig.setRootClass(ShopCar.class);
			// 声明name 用来接收cookie中的key,value 用来接收cookie值
			String name = "", value = "";
			for (Cookie cookie : cookies) {
				name = cookie.getName();
				if (ShopCarService.COOKIE_NAME.equals(name)) {
					value = cookie.getValue();
					value = URLDecoder.decode(value, "UTF-8");
					System.out.println("value: " + value);
					JSONArray jArray = JSONArray.fromObject(value);

					list = (List<ShopCar>) JSONSerializer.toJava(jArray, jConfig);
					for (ShopCar car : list) {
						if (id.equals(car.getIdPro()) && car.getSpec().equals(spec)) {
							return StringUtils.sumOfString(car.getNum(), num);

						}

					}

				} 
			}
			return num;
		} else {
			return num;
		}
	}

}
